package com.goldenautumn.prefabricate.activity.mine.adapter

import android.content.Context
import android.view.ViewGroup
import android.view.ViewTreeObserver
import androidx.core.content.ContextCompat
import androidx.recyclerview.widget.LinearLayoutManager
import com.chad.library.adapter4.BaseQuickAdapter
import com.chad.library.adapter4.viewholder.DataBindingHolder
import com.goldenautumn.prefabricate.R
import com.goldenautumn.prefabricate.business.entity.ExpressItem
import com.goldenautumn.prefabricate.databinding.ItemLogisticsBinding

class LogisticsAdapter : BaseQuickAdapter<ExpressItem, DataBindingHolder<ItemLogisticsBinding>>() {

    var selectPosition = -1

    override fun onBindViewHolder(holder: DataBindingHolder<ItemLogisticsBinding>, position: Int, item: ExpressItem?) {
        if (item == null) return
        var mBinding = holder.binding
        var progress = LogisticsProgressAdapter(position == 0)
        mBinding.logisticsProgress.layoutManager = LinearLayoutManager(context)
        mBinding.logisticsProgress.adapter = progress
        progress.submitList(item.items)

        mBinding.logisticsProgress.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                mBinding.logisticsProgress.viewTreeObserver.removeOnGlobalLayoutListener(this)
                var lp = mBinding.time.layoutParams
                lp.height = mBinding.logisticsProgress.height
                mBinding.time.layoutParams = lp
            }
        })

        item.icon?.let { mBinding.icon.setImageResource(it) }
        mBinding.status.text = "${item.statusName}"
        mBinding.status.setTextColor(ContextCompat.getColor(context,if(position == 0)R.color.colorPrimary else qncx.external.common.R.color.color_999999))
    }

    override fun onCreateViewHolder(
        context: Context,
        parent: ViewGroup,
        viewType: Int
    ): DataBindingHolder<ItemLogisticsBinding> {
        return DataBindingHolder(R.layout.item_logistics, parent)
    }
}